﻿using Prism.Events;
using Module.Common.Common;
using Module.Common.PubEvent;
using System.Windows.Controls;

namespace Module.Common.Views.Marks
{
    /// <summary>
    /// Interaction logic for CurrentLoaction
    /// </summary>
    public partial class CurrentLoaction : UserControl
    {
        private bool _isQiPanFanZhuan;
        private bool IsQiPanInvers
        {
            get { return _isQiPanFanZhuan; }
            set
            {
                _isQiPanFanZhuan = value;
            }
        }
        private int Col { get; set; }    // 路径点的列坐标
        private int Row { get; set; }    // 路径点的行坐标
        public string ModuleName { get; set; }
        private readonly IEventAggregator Ea;
        public CurrentLoaction(IEventAggregator eventAggregator)
        {
            InitializeComponent();
            Ea = eventAggregator;
            Visibility = System.Windows.Visibility.Hidden;
            Ea.GetEvent<SystemMessageEvent>().Subscribe(SystemMessageProcess);
            Ea.GetEvent<ModuleMessageEvent>().Subscribe(ModuleMessageProcess,
                ThreadOption.UIThread,
                false,
                filter => filter.ModuleName == this.ModuleName);
        }

        private void SystemMessageProcess(SystemMessage obj)
        {
            switch (obj.MessageName)
            {
                case "qipanInverse":  // 棋盘翻转消息
                    IsQiPanInvers = !IsQiPanInvers;
                    SetPosition(Col, Row);
                    break;
                
                default:
                    break;
            }
        }
        /// <summary>
        /// 订阅消息处理
        /// </summary>
        /// <param name="obj">消息体</param>
        private void ModuleMessageProcess(ModuleMessage obj)
        {
            switch (obj.MessageName)
            {
                case "qiziGetClicked":
                    SetPosition(obj.NewCol, obj.NewRow);
                    Visibility = System.Windows.Visibility.Visible;
                    break;
                case "qiziLocationHasChanged":
                    SetPosition(obj.NewCol, obj.NewRow);
                    Visibility = System.Windows.Visibility.Visible;
                    break;
                case "goBackStepLunch":
                    SetPosition(obj.BufferCol, obj.BufferRow);
                    break;
                case "reStart": // 重新开局
                    Visibility = System.Windows.Visibility.Hidden;
                    break;

                default: break;
            }
        }
        /// <summary>
        /// 设置可移动点位置
        /// </summary>
        /// <param name="x">行</param>
        /// <param name="y">列</param>
        private void SetPosition(int x, int y)
        {
            Col = x;
            Row = y;
            if (IsQiPanInvers) // 如果棋盘翻转为上红下黑，则进行坐标转换
            {
                x = 8 - x;
                y = 9 - y;
            }
            SetValue(Canvas.LeftProperty, GlobalData.GetQiPanGrid_X(x));
            SetValue(Canvas.TopProperty, GlobalData.GetQiPanGrid_Y(y));
        }
    }
}
